<template>
  <div class="newAddress">
    <!-- 返回 -->

    <van-nav-bar
      title="新增地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <van-address-edit
      :area-list="areaList"
      show-postal
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import areaList from '../assets/js/area'
import { Toast } from 'vant';
export default {
  name: "NewAddress",
  data() {
    return {
      areaList,
      searchResult: []
    };
  },

  // 方法
  methods: {
    onSave(content) {

        console.log('我是表单里面的 内容',content);
        
        // 添加地址
        this.axios({
            method:"post",
            url:"/address",
            data:{
               userId:localStorage.getItem('token'),
               name: content.name,
               phone:content.tel,
               area:content.province+content.city+content.county,
               detialArea:content.addressDetail,
               mail:content.postalCode,
               defaultAddress:content.isDefault
            }
        }).then(res=>{
            console.log(res);
            if(res.data.stutas==1999){
                // 如果添加成功就返回去
                Toast('添加成功')
                this.$router.go(-1)
            }
        }).catch(err=>{

        })

    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    },
    onClickLeft(){
        // 返回上一级
        this.$router.go(-1)
    }
  }
};
</script>

<style lang="less" scoped>
</style>